package br.com.ps.bombertournament.game.util;

import java.util.HashMap;
import java.util.Map;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.files.FileHandle;

/**
 * 
 * @author Rennan Stefan Boni - rennanboni@gmail.com
 */
public class SoundCache {
	
	private static final Map<String, Sound> sounds = new HashMap<String, Sound>();

	public static boolean play(String path) {
		boolean result = false;
		
		if (sounds.containsKey(path) == false) {
			prepare(path);
		}
		
		if (sounds.containsKey(path)) {
			sounds.get(path).play();
			result = true;
		}
		
		return result;
	}
	
	public static void dispose() {
		if (sounds != null) {
			for (String key : sounds.keySet()) {
				sounds.get(key).dispose();
			}
		}
		
		sounds.clear();
	}

	public static boolean prepare(String path) {
		boolean result = false;
		
		FileHandle file = Gdx.files.internal(path);
		if (file != null) {
			Sound sound = Gdx.audio.newSound(file);
			
			sounds.put(path, sound);

			result = true;
		}
		
		return result;
	}
}
